package cn.fantasy.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @author ET
 * @description 使用线程的Semaphore(信号量)方法
 * @date 2023/3/10 15:13
 * @Version V1.0
 */
public class ThreadOrderEight {
    private static Semaphore semaphore1 = new Semaphore(1);
    private static Semaphore semaphore2 = new Semaphore(1);
    public static void main(String[] args) {
        final Thread thread1 = new Thread(() -> {
            System.out.println("one");
            semaphore1.release();
        });

        final Thread thread2 = new Thread(() -> {
            try {
                semaphore1.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("two");
            semaphore2.release();
        });

        final Thread thread3 = new Thread(() -> {
            try {
                semaphore2.acquire();
                thread2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            semaphore2.release();
            System.out.println("three");
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }

}
